<?php

class Application_Model_ProductMapper{

	protected $_dbTable;

	public function setDbTable($dbTable)
	{
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}

	public function getDbTable()
	{
		if (null === $this->_dbTable) {
			$this->setDbTable('Application_Model_DbTable_Product');
		}
		return $this->_dbTable;
	}
	
	public function save(Application_Model_Product $product)
	{
		
		
		$objectMapper = new Application_Model_ObjectMapper();
		$row = $objectMapper->save($product);
		//TODO set the object save function to return the id of the object saved
		//$where = array('name = ?' => (string)$object->getName());
		$product->setObjectId($row);
		
		$data = array(
				'id_object' => $product->getObjectId(),
				'model'   => $product->getModel(),
				'brand' => $product->getBrand(),
				'productID' => $product->getProductIdNum(),
				'manufacturer' => $product->getManufacturer(),
		);
		
		if (null === ($id = $product->getProductId())) {
			unset($data['id_product']);
			return $this->getDbTable()->insert($data);
		} else {
			$this->getDbTable()->update($data, array('id_product = ?' => $id));
			return $id;
		}
		
		
	}
	
	public function find($id, Application_Model_Product $product)
	{
		$result = $this->getDbTable()->find($id);
		if (0 == count($result)) {
			return;
		}
		$row = $result->current();
		$product->setProductId($row->id_product);
		$product->setObjectId($row->id_object);
		$product->setModel($row->model);
		$product->setBrand($row->brand);
		$product->setProductIdNum($row->productID);
		$product->setManufacturer($row->manufacturer);
		$objectMapper = new Application_Model_ObjectMapper();
		$product = $objectMapper->find($product->getObjectId(), $product);
		
		return $product;
	}
	
	public function delete($id)
	{

		$product = new Application_Model_Product();
		$objectMapper = new Application_Model_ObjectMapper();
		$product = $this->find($id, new Application_Model_Product());
		
		$result = $this->getDbTable()->find($id);
		if (0 != count($result)) {
			$this->getDbTable()->delete(array('id_product = ?' => $id));
		}
		
		$objectMapper->delete($product->getObjectId());
		
	
	}
	
	public function indexBuilder(Zend_Search_Lucene_Document $doc, Application_Model_Product $product)
	{
		$objectMapper = new Application_Model_ObjectMapper();
		$doc->addField(Zend_Search_Lucene_Field::Text('model', $product->getModel(), 'utf-8'));
		$doc->addField(Zend_Search_Lucene_Field::Text('brand', $product->getBrand(), 'utf-8'));
		$doc->addField(Zend_Search_Lucene_Field::unStored('productIdNum', $product->getProductIdNum(), 'utf-8'));
		$doc->addField(Zend_Search_Lucene_Field::unStored('manufacturer', $product->getManufacturer(), 'utf-8'));
		$doc = $objectMapper->indexBuilder($doc, $product);
	
		return $doc;
	}
}
